import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Test {
    /**
     * 查询
     * @param args
     */
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.连接数据库
        Connection connection = dataSource.getConnection();

        //3.构造sql
        String sql = "select * from test";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行sql
        ResultSet resultSet = statement.executeQuery();

        //5.遍历执行
        while(resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: " + id + " name: " + name);
        }

        //6.释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
    /**
     * 删除
     * @param args
     */
    public static void main3(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.连接数据库
        Connection connection = dataSource.getConnection();

        //3.构造mysql语句
        String sql = "delete from test where name = '张三三'";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //执行sql语句
        int n = preparedStatement.executeUpdate();
        System.out.println("影响行数: " + n);
        //5.释放资源
        preparedStatement.close();
        connection.close();
    }
    /**
     * 修改
     * @param args
     */
    public static void main2(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSl=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.连接数据库
        Connection connection = dataSource.getConnection();

        //3.构造sql语句
        String sql = "update test set name = '张三三' where id = 1";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //4.执行sql语句
        int n = preparedStatement.executeUpdate();
        System.out.println("影响行数: " + n);

        //5.释放资源
        preparedStatement.close();
        connection.close();
    }

    /**
     * 插入
     * @param args
     * @throws SQLException
     */
    public static void main1(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        //1.创建数据源
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");
        //  设置数据库名
        ((MysqlDataSource) dataSource).setUser("root");
        //  设置数据库密码
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.和数据库建立连接
        //通信模式:
        //1.有连接->需要进行连接
        Connection connection = dataSource.getConnection();
        //2.无连接->无需连接

        //3.构造sql语句
        //静态写法
//        String sql = "insert into test values(1,'张三')";
        //动态写法 1.直接拼接
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号: ");
        int id = scanner.nextInt();
        System.out.println("请输入姓名: ");
        String str = scanner.next();
//        String sql = "insert into test values("+ id + ",'" + str + "')";
//        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //2.使用数据库API进行校验
        String sql = "insert into test values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,str);
        //4.执行sql
        int n = preparedStatement.executeUpdate();//n->影响行数
        System.out.println("n: " + n);

        //5.收尾操作(先创建的后释放)->释放连接对象->语句对象
        preparedStatement.close();
        connection.close();
    }
}
